/*
 * Copyright 2012-2017 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ccxc.question.user.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ccxc.question.commons.core.DaoCode;
import com.ccxc.question.commons.core.Parameter;
import com.ccxc.question.commons.core.PublicErrorCode;
import com.ccxc.question.commons.exception.CommonException;
import com.ccxc.question.commons.utils.GetWeChatOpenId;
import com.ccxc.question.commons.utils.Sample;
import com.ccxc.question.user.dao.UserDao;
import com.ccxc.question.user.dto.User;
import com.ccxc.question.user.vo.OneValueVO;
import com.ccxc.question.user.vo.WUser;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>微信用户操作逻辑控制层</p>
 */
@Service
@Transactional
@Slf4j
public class UserService implements DaoCode, Parameter {

    @Autowired
    private UserDao userDao;


    /**
     * <p>获取openid</p>
     *
     * @param code 传入code
     * @return
     * @Title: getWeixinOpenInfo
     * @version:V0.1
     * @return:String 传出openid
     */
    public String getWeixinOpenInfo(String code) {
        return GetWeChatOpenId.getWeixinOpenInfo(code);
    }

    /**
     * <p>先根据code查询是否已经注册，若注册跳转检测界面，若未注册就进行注册</p>
     */
    public String findUser(String openid, WUser user) {
        if (null != openid) {
            boolean listuserservice = userDao.findUser(openid);

            if (listuserservice) {
                int number = userDao.reUser(user, openid);
                if (ADD_ERROR != number) {
                    log.info("注册用户成功");
                }
            } else {
                return User.getToken();
            }
        }
        return null;
    }

    /**
     * <p>人脸注册</p>
     */
    public String faceRe(String imageBase) {
        if (null != imageBase) {
        //	if (Sample.jiancheFace(imageBase)) {
        		
				return Sample.faceRe(imageBase);
			//}
        	//else {
			//	return "太丑了，都快开不出是个人了,重新照吧！！！！";
			//}
				      
        }
        new CommonException(PublicErrorCode.PARAM_EXCEPTION.getIntValue(), "照片异常");
        return null;
    }

    /**
     * <p>人脸注册的时候将人脸的token写入数据库</p>
     */
    public void addToken(OneValueVO oneValueVO) {
        int number = userDao.addToken(oneValueVO);
        if (ADD_ERROR == number) {
            new CommonException(PublicErrorCode.SAVE_EXCEPTION.getIntValue(), "更新token失败");
        }
    }

    /**
     * <p>人脸检测</p>
     *
     * @param imageBase
     * @return
     * @Title: searchFace
     * @version:V0.1
     * @return:String
     */
    public String searchFace(String imageBase) {
        if (null != imageBase) {
        	//if (Sample.jiancheFace(imageBase)) {
				return Sample.searchFace(imageBase);
			//}
            //else {
			//	return "太丑了，都快开不出是个人了,重新照吧！！！！";
			//}
        }
        new CommonException(PublicErrorCode.PARAM_EXCEPTION.getIntValue(), "照片异常");
        return null;
    }


    public String findUser2(String openid) {
        if (null != openid) {
            String userid = userDao.findUser2(openid);
            return userid;
        }
        return null;
    }
}
